package org.rency.mq.activemq.listener.failover.impl;

import org.rency.mq.activemq.listener.failover.RetryStrategy;
import org.rency.mq.activemq.request.MQRequest;

public class DurationRetryStrategy extends AbstractRetryStrategy implements RetryStrategy {
	private long duration = 120;//2880;

	public void setDuration(long duration) {
		if(duration > 0){
			this.duration = duration;
		}
	}

	public long getDuration() {
		return duration;
	}

	public void retry(MQRequest request) {
		if (!request.containsProperty("duration")) {
			request.addProperty("duration", System.currentTimeMillis()
					+ duration * 60000);
		}

		super.retry(request);
	}

	@Override
	protected boolean needRetry(MQRequest request) {
		if (duration <= 0)
			return true;

		if (request.containsProperty("duration")
				&& Long.parseLong(request.getProperty("duration").toString()) > System
						.currentTimeMillis())
			return true;

		return false;
	}

}
